import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworks
 * Date: 8/21/13
 * Time: 11:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class Calculator {

    public int add(String numbers) throws NegativeNumberException {
        int result = 0;
        if (numbers.isEmpty())
            result = 0;
        else if (numbers.length() < 3 && Integer.parseInt(numbers) < 0)
            throw new NegativeNumberException(numbers);
        else if (numbers.length() < 3)
            result = Integer.parseInt(numbers);
        else if (numbers.substring(0,2).equals("//"))
            result = parseByCharacter(numbers.substring(numbers.indexOf("\n") + 1), numbers.substring(3, numbers.indexOf("]")));
        else if (numbers.contains(",") && numbers.contains("\n"))
            result = parseCommasAndNewLines(numbers);
        else if (numbers.contains(","))
            result = parseByCharacter(numbers, ",");
        else if (numbers.contains("\n"))
            result = parseByCharacter(numbers, "\n");
        return result;
    }

    private int parseByCharacter(String numbers, String character) throws NegativeNumberException {
        int result = 0;
        LinkedList<String> negativeNumbers = new LinkedList<String>();
        String[] parsedNumbers = numbers.split(character);
        for (String number : parsedNumbers)  {
            if (number.isEmpty() && character.equals("-"))
                throw new NegativeNumberException(findNegatives(numbers));
            else if (Integer.parseInt(number) < 0)
                negativeNumbers.add(number);
            else if (Integer.parseInt(number) < 1001)
                result += Integer.parseInt(number);
        }
        if (!negativeNumbers.isEmpty())
            throw new NegativeNumberException(negativeNumbers);
        return result;
    }

    private String findNegatives(String numbers) {

        return null;
    }


    public int parseCommasAndNewLines(String numbers) throws NegativeNumberException {
        int result = 0;
        String[] parsedNumbers = numbers.split(",");
        for (String number : parsedNumbers) {
            result += add(number);
        }
        return result;
    }


    public static void main(String[] args) {
        Calculator calc = new Calculator();
        String numbers = "//[-]\n" +
                "[1--2--3]";
        try {
            int result = calc.add(numbers);
            System.out.println(result);
        } catch (NegativeNumberException e) {
            e.printStackTrace();
        }


    }

}
